package com.bijiawang.user.controller;

import com.bijiawang.user.domin.Favorites;
import com.bijiawang.user.domin.Nfavorites;
import com.bijiawang.user.service.FavoritesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;

@RestController
public class FavoritesController {
    @Autowired
    FavoritesService favoritesService;

    /*
        用户点击收藏后所执行的往数据库插入数据的方法
     String goodsName,String goodsUrl,String goodsPrice,String shopName的名称要分别与
    路径上的参数名称一一对应
    * */
    @PostMapping("/insertFavorites")
    public void save(@RequestParam("username")String username, @RequestParam("goodsname")String goodsName,
                     @RequestParam("url") String goodsUrl,@RequestParam("price") Double goodsPrice,@RequestParam("shopname") String shopName){
       favoritesService.saveToFavorites(username,goodsName,goodsUrl,goodsPrice,shopName);
       favoritesService.saveToNfavorites(username,goodsName,goodsUrl,goodsPrice,shopName);
       //favoritesService.ListeningPrice(goodsUrl,shopName);
    }

    //通过用户名查询该用户收藏的所有商品
    @GetMapping("/all")
    public List getFavorites(@AuthenticationPrincipal UserDetails userDetails){
        List<Favorites> all = favoritesService.getFavorites(userDetails.getUsername());
        List list = new ArrayList();
        for (int i=0;i<all.size();i++) {
            int goodsId = all.get(i).getGoodsId();
            List<Nfavorites> nfavorites = favoritesService.getAllByOne(goodsId);
            list.add(nfavorites);
        }
        return list;
    }
}
